package org.wheel.plugins.toolkit.sql.capability;

import org.wheel.plugins.toolkit.sql.enums.CompareType;
import org.wheel.plugins.toolkit.sql.helper.SFunction;

/**
 * 列与列比较运算能力
 * 场景：列 ↔ 列
 * 示例：eq(User::getId, Order::getUserId)
 * @param <W> 具体 Wrapper 类型，用于链式返回自身
 * @param <T> 主实体类型
 */
public interface LambdaColumnCompare<W extends LambdaColumnCompare<W, T>, T> {

    /* ==================== 常用快捷方法 ==================== */

    /**
     * 等于 =
     */
    default <J, R> W eq(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return eq(leftCol, null, rightCol);
    }
    default <J, R> W eq(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return eq(null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W eq(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.EQ, leftTableAlias, leftCol, rightTableAlias, rightCol);
    }

    /**
     * 不等于 &lt;&gt;
     */
    default <J, R> W ne(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return ne(leftCol, null, rightCol);
    }
    default <J, R> W ne(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return ne(null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W ne(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.NE, null, leftCol, null, rightCol);
    }

    /**
     * 大于 &gt;
     */
    default <J, R> W gt(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return gt(leftCol, null, rightCol);
    }
    default <J, R> W gt(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return gt(null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W gt(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.GT, null, leftCol, null, rightCol);
    }

    /**
     * 大于等于 &gt;=
     */
    default <J, R> W ge(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return ge(leftCol, null, rightCol);
    }
    default <J, R> W ge(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return ge(null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W ge(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.GE, leftTableAlias, leftCol, rightTableAlias, rightCol);
    }

    /**
     * 小于 &lt;
     */
    default <J, R> W lt(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return lt(leftCol, null, rightCol);
    }
    default <J, R> W lt(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return lt(null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W lt(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.LT, leftTableAlias, leftCol, rightTableAlias, rightCol);
    }

    /**
     * 小于等于 &lt;=
     */
    default <J, R> W le(SFunction<T, R> leftCol, SFunction<J, R> rightCol){
        return le(leftCol, null, rightCol);
    }
    default <J, R> W le(SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return le( null, leftCol, rightTableAlias, rightCol);
    }
    default <J, R> W le(String leftTableAlias, SFunction<T, R> leftCol, String rightTableAlias, SFunction<J, R> rightCol){
        return compareCol(CompareType.LE, leftTableAlias, leftCol, rightTableAlias, rightCol);
    }

    /* ==================== 统一原子方法 ==================== */

    /**
     * 通用列 vs 列比较，所有快捷方法内部转调这里
     *
     * @param type     比较类型
     * @param leftCol  左列 Lambda
     * @param rightCol 右列 Lambda（任意实体）
     * @param <J><R>      列返回值类型
     * @return 链式自身
     */
    <J> W compareCol(CompareType type, String leftTableAlias, SFunction<T, ?> leftCol, String rightTableAlias,SFunction<J, ?> rightCol);
}
